package com.ruoyi.web.controller.system;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.utils.StringUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.YhNotice;
import com.ruoyi.system.service.IYhNoticeService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 各类型公告展示Controller
 * 
 * @author ruoyi
 * @date 2023-06-06
 */
@Api(tags = "各类型公告展示")
@RestController
@RequestMapping("/yh/notice")
public class YhNoticeController extends BaseController
{
    @Autowired
    private IYhNoticeService yhNoticeService;

    /**
     * 查询各类型公告展示列表
     */
    @ApiOperation("获取各类型公告展示列表")
    @PreAuthorize("@ss.hasPermi('yh:notice:list')")
    @GetMapping("/list")
    public TableDataInfo list(YhNotice yhNotice)
    {
        startPage();
        List<YhNotice> list = yhNoticeService.list(yhNotice.queryWrapper());
        return getDataTable(list);
    }

    /**
     * 导出各类型公告展示列表
     */
    @ApiOperation("导出各类型公告展示列表")
    @PreAuthorize("@ss.hasPermi('yh:notice:export')")
    @Log(title = "各类型公告展示", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, YhNotice yhNotice)
    {
        List<YhNotice> list = yhNoticeService.list(yhNotice.queryWrapper());
        ExcelUtil<YhNotice> util = new ExcelUtil<YhNotice>(YhNotice.class);
        util.exportExcel(response, list, "各类型公告展示数据");
    }

    /**
     * 获取各类型公告展示详细信息
     */
    @ApiOperation("获取各类型公告展示详细")
    @ApiImplicitParam(name = "id", value = "各类型公告展示ID", required = true, dataType = "String", paramType = "path")
    @PreAuthorize("@ss.hasPermi('yh:notice:list')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        YhNotice notice = yhNoticeService.getById(id);
        return success(notice);
    }

    /**
     * 按类型获取公告详情
     */
    @ApiOperation("按类型获取公告详情")
    @ApiImplicitParam(name = "type", value = "公告类型", required = true, dataType = "String", paramType = "path")
    @PreAuthorize("@ss.hasPermi('yh:notice:list')")
    @GetMapping(value = "/getByType/{type}")
    public AjaxResult getByType(@PathVariable("type") String type)
    {
        YhNotice notice = yhNoticeService.getOne(new LambdaQueryWrapper<YhNotice>()
                .eq(YhNotice::getType,type));
        notice.setPageView(notice.getPageView() + 1);
        yhNoticeService.updateById(notice);
        return success(notice);
    }


    /**
     * 新增各类型公告展示
     */
    @ApiOperation("新增各类型公告展示")
    @PreAuthorize("@ss.hasPermi('yh:notice:add')")
    @Log(title = "各类型公告展示", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody YhNotice yhNotice)
    {
        YhNotice yhNotice1 = yhNoticeService.selectByType(yhNotice.getType());
        if(StringUtils.isNotNull(yhNotice1)){
            AjaxResult.error("该类型公告已存在");
        }
        return toAjax(yhNoticeService.save(yhNotice));
    }

    /**
     * 修改各类型公告展示
     */
    @ApiOperation("更新各类型公告展示")
    @PreAuthorize("@ss.hasPermi('yh:notice:edit')")
    @Log(title = "各类型公告展示", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody YhNotice yhNotice)
    {
        return toAjax(yhNoticeService.editById(yhNotice));
    }

    /**
     * 删除各类型公告展示
     */
    @ApiOperation("删除各类型公告展示")
    @ApiImplicitParam(name = "ids", value = "各类型公告展示ID集合", required = true, dataType = "String", paramType = "path")
    @PreAuthorize("@ss.hasPermi('yh:notice:remove')")
    @Log(title = "各类型公告展示", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<String> ids)
    {
        return toAjax(yhNoticeService.removeByIds(ids));
    }


    /**
     * 各类型公告浏览量
     */
    @ApiOperation("各类型公告浏览量")
    @PreAuthorize("@ss.hasPermi('yh:notice:list')")
    @GetMapping("/pageView")
    public AjaxResult pageView()
    {
        Map<String, Long> map = yhNoticeService.list().stream().collect(Collectors.toMap(YhNotice::getType, YhNotice::getPageView));
        YhNotice yhNotice = yhNoticeService.getOne(new LambdaQueryWrapper<YhNotice>()
                .eq(YhNotice::getType, "HOME_NOTICE"));
        map.put("SERVICE_CENTER", yhNotice.getServiceCenterPageView() == null ? 0 : yhNotice.getServiceCenterPageView());
        return success(map);
    }

    /**
     * 统计客服中心浏览量
     */
    @ApiOperation("统计客服中心浏览量")
    @PreAuthorize("@ss.hasPermi('yh:notice:list')")
    @GetMapping("/serviceCenterPageView")
    public AjaxResult serviceCenterPageView()
    {
        YhNotice yhNotice = yhNoticeService.getOne(new LambdaQueryWrapper<YhNotice>()
                .eq(YhNotice::getType, "HOME_NOTICE"));
        yhNotice.setServiceCenterPageView(yhNotice.getServiceCenterPageView() == null ? 0 : yhNotice.getServiceCenterPageView() + 1);
        yhNoticeService.updateById(yhNotice);
        return success();
    }
}
